package com.ebo.apicloud;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ebo.agora.openvcall.AGApplication;

import java.util.ArrayList;
import java.util.List;

import io.agora.openvcall.R;
public class StartActivity extends Activity implements View.OnClickListener,
        ViewPager.OnPageChangeListener {
    private ViewPager vp;
    private MyViewPagerAdapter vpAdapter;
    private List<View> views;
    private Button button;
    // 引导图片资源
    private static final int[] pics = {R.drawable.initiation1, R.drawable.initiation2, R.drawable.initiation3};

    // 记录当前选中位置
    private int currentIndex;
    private View item;
    private LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.guide);

        inflater = LayoutInflater.from(StartActivity.this);
        views = new ArrayList<View>();

        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // 初始化引导图片列表
        for (int i = 0; i < pics.length; i++) {
            item = inflater.inflate(R.layout.home_viewpager_item, null);
            views.add(item);

        }

        vp = (ViewPager) findViewById(R.id.viewpager);
        // 初始化Adapter
        vpAdapter = new MyViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        // 绑定回调
        vp.setOnPageChangeListener(this);

        // 初始化底部小点
        initIndicator();
        AGApplication.init();
    }


    /**
     * 设置当前的引导页
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }

        vp.setCurrentItem(position);
    }

    // 当滑动状态改变时调用
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    // 当当前页面被滑动时调用
    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    boolean canSkip;

    // 当新的页面被选中时调用
    @Override
    public void onPageSelected(int arg0) {
        // 设置底部小点选中状态
        for (int i = 0; i < indicator_imgs.length; i++) {
            if (i == arg0) { // 初始化第一个为选中状态
                indicator_imgs[i]
                        .setBackgroundResource(R.drawable.indicator_focused);
            } else {
                indicator_imgs[i].setBackgroundResource(R.drawable.indicator);
            }
        }
        if (arg0 == indicator_imgs.length - 1) {
            canSkip = true;
            // button.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        int position = (Integer) v.getTag();
        setCurView(position);
    }

    private ImageView[] indicator_imgs = new ImageView[pics.length];// 存放引到图片数组

    /**
     * 初始化引导图标 动态创建多个小圆点，然后组装到线性布局里
     */
    private void initIndicator() {
        ImageView imgView;
        View v = findViewById(R.id.indicator);// 线性水平布局，负责动态调整导航图标
//        v.setVisibility(View.GONE);
        for (int i = 0; i < pics.length; i++) {
            imgView = new ImageView(this);
            LinearLayout.LayoutParams params_linear = new LinearLayout.LayoutParams(
                    15, 15);
            params_linear.setMargins(15, 15, 0, 0);
            imgView.setLayoutParams(params_linear);
            indicator_imgs[i] = imgView;

            if (i == 0) { // 初始化第一个为选中状态
                indicator_imgs[i]
                        .setBackgroundResource(R.drawable.indicator_focused);
            } else {
                indicator_imgs[i].setBackgroundResource(R.drawable.indicator);
            }
            ((ViewGroup) v).addView(indicator_imgs[i]);
        }
    }

    class MyViewPagerAdapter extends PagerAdapter {
        private ImageView image;
        // 界面列表
        private List<View> views;

        public MyViewPagerAdapter(List<View> views) {
            this.views = views;
        }

        // 销毁arg1位置的界面
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(views.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub
        }

        // 获得当前界面数
        @Override
        public int getCount() {
            if (views != null) {
                return views.size();
            }
            return 0;
        }

        // 初始化arg1位置的界面
        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(views.get(arg1), 0);
            return views.get(arg1);
        }

        @Override
        public Object instantiateItem(final ViewGroup container,
                                      final int position) {
            View view = views.get(position);
            image = ((ImageView) view.findViewById(R.id.image));
            image.setScaleType(ImageView.ScaleType.CENTER_CROP);
            ViewGroup.LayoutParams lp = image.getLayoutParams();
            lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
            lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            image.setLayoutParams(lp);
            image.setImageResource(pics[position]);
            container.removeView(views.get(position));
            container.addView(views.get(position));
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position == pics.length - 1)
                    {//
//                        startActivity(new Intent(StartActivity.this, ConversationLoginActivity.class));
                        startActivity(new Intent(StartActivity.this, H5Activity.class));
                        finish();
                    }
                }
            });
            return views.get(position);
        }

        // 判断是否由对象生成界面
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return (arg0 == arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

    }


}
